package service

import (
	"context"
	"github.com/micro/go-micro/v2/errors"
	"micro-grpc-server/services/wechat/internal/resource"
	pb "micro-grpc-server/services/wechat/proto"
)

// 微信素材库新增
func (s *WechatService) MediaCreate(ctx context.Context, rq *pb.WechatMedia, resp *pb.WechatMediaResponse) error {
	media, err := s.dao.CreateMedia(rq)
	if err != nil {
		return err
	}
	var r = &resource.UserResource{}
	resp.Data = r.MediaToProto(media)
	return nil
}

// 微信素材库更新
func (s *WechatService) MediaUpdate(ctx context.Context, rq *pb.WechatMedia, resp *pb.WechatMediaResponse) error {
	if rq.Id < 0 {
		return errors.BadRequest(s.conf.ServerName, "参数错误")
	}
	media, err := s.dao.UpdateMedia(rq.Id, rq)
	if err != nil {
		return err
	}
	var r = &resource.UserResource{}
	resp.Data = r.MediaToProto(media)
	return nil
}

// 微信素材库删除
func (s *WechatService) MediaDelete(ctx context.Context, rq *pb.WechatMedia, resp *pb.EmptyResponse) error {
	if rq.Id < 0 {
		return errors.BadRequest(s.conf.ServerName, "参数错误")
	}
	if err := s.dao.DeleteMedia(rq.Id); err != nil {
		return err
	}
	return nil
}
